package cn.yunhe.service.impl;

import cn.yunhe.dao.UserDao;
import cn.yunhe.entity.User;
import cn.yunhe.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    @Override
    public int addUser(Map map) {
        User user = (User) map.get("user");
        if ("".equals(user.getUserName()) || "".equals(user.getTrueName()) || "".equals(user.getPassword()) ||
                "".equals(user.getNationalId()) || "".equals(user.getEmail())) {
            return 0;
        }
        return userDao.addUser(map);
    }

    @Override
    public int addUser2Role(Map map) {
        return userDao.addUser2Role(map);
    }

    @Override
    public User userLogin(String username) {
        return userDao.userLogin(username);
    }

    @Override
    public int getUserCount(User user) {
        return userDao.getUserCount(user);
    }

    @Override
    public List<User> getAllStudent(User user) {
        return userDao.getAllStudent(user);
    }

    @Override
    public List<User> getAllTeacher(User user) {
        return userDao.getAllTeacher(user);
    }

    @Override
    public List<User> getAllAdmin(User user) {
        return userDao.getAllAdmin(user);
    }

    @Cacheable(value = "userList", key = "#p0")
    @Override
    public Page getUserByPage(Map<String, Object> map) {
        Page page = new Page();
        //设置 总条数
        int totalNum = userDao.getUserCount((User) map.get("user"));
        page.setTotal(totalNum);
        // 获取页大小
        int pageSize = (int) map.get("pageSize");
        // 获取当前页
        int currPage = map.get("currPage") == null ? 1 : (int) map.get("currPage");
        //计算总页数
        int totalPage = (totalNum / pageSize) + (totalNum % pageSize == 0 ? 0 : 1);
        page.setPageNum(totalPage);
        //启用分页
        page = PageHelper.startPage(currPage, pageSize);
        int role = (int) map.get("role");
        User user = (User) map.get("user");
        switch (role){
            case 1:
                getAllAdmin(user);
                break;
            case 2:
                getAllTeacher(user);
                break;
            case 3:
                getAllStudent(user);
                break;
        }
        return page;
    }

    @Override
    public int updUserPwd(User user) {
        String newPwd = new SimpleHash("MD5", user.getPassword(), user.getUserName()).toString();
        user.setPassword(newPwd);
        return userDao.updUserPwd(user);
    }

    @Override
    public User getUserById(int id) {
        return userDao.getUserById(id);
    }

    @Override
    public int updUserInfo(User user) {
        return userDao.updUserInfo(user);
    }

    @Override
    public int updUserIsDisable(int userId) {
        User user = userDao.getUserById(userId);
        if (user.getEnabled() == 1) {
            return userDao.updUserDisable(userId);
        } else {
            return userDao.updUserUnDisable(userId);
        }
    }

    @Override
    public int studentCount() {
        return userDao.studentCount();
    }

    @Override
    public User getUserByName(String username) {
        return userDao.getUserByName(username);
    }


    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

}
